﻿Public Class FViewActivePairUpTickets

    Public Shadows Sub Show(ByVal owner As IWin32Window)
        ListView1.BeginUpdate()
        For Each clsBuddy As Entities.Buddy In Framework.Buddies.Manager.GetAll
            If Not String.IsNullOrEmpty(clsBuddy.PairUpPinCode) AndAlso clsBuddy.PairUpValidUntil > Now.ToUniversalTime Then
                With ListView1.Items.Add(clsBuddy.NameLocal)
                    .Tag = clsBuddy
                End With
            End If
        Next
        If ListView1.Items.Count > 0 Then ListView1.Items(0).Selected = True
        ListView1.EndUpdate()
        Me.Icon = Framework.My.Resources.Buddy
        txtHost.Text = Framework.Net.Settings.Host
        txtPort.Text = Framework.Net.Settings.Port.ToString
        MyBase.Show(owner)
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        If ListView1.SelectedItems.Count = 1 Then
            Dim clsBud As Entities.Buddy = CType(ListView1.SelectedItems(0).Tag, Entities.Buddy)
            txtBuddyNr.Text = clsBud.Id.ToString
            txtPinCode.Text = clsBud.PairUpPinCode
            txtValidUntil.Text = clsBud.PairUpValidUntil.ToString("g")
            Button1.Enabled = True
        Else
            Button1.Enabled = False
            txtBuddyNr.Text = String.Empty
            txtPinCode.Text = String.Empty
            txtValidUntil.Text = String.Empty
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ListView1.SelectedItems.Count = 1 Then
            Dim clsBud As Entities.Buddy = CType(ListView1.SelectedItems(0).Tag, Entities.Buddy)
            Dim clsSB As New System.Text.StringBuilder
            clsSB.Append("Buddy#:")
            clsSB.AppendLine(clsBud.Id.ToString)
            clsSB.Append("PinCode:")
            clsSB.AppendLine(clsBud.PairUpPinCode)
            clsSB.Append("Host:")
            clsSB.AppendLine(Framework.Net.Settings.Host)
            clsSB.Append("Port:")
            clsSB.AppendLine(Framework.Net.Settings.Port.ToString)
            Clipboard.Clear()
            Clipboard.SetText(clsSB.ToString)
            MessageBox.Show(Me, "Pair up ticket copied to clipboard", "Pair Up")
        End If
    End Sub
End Class